//
//  LGetCodeView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LGetCodeView.h"

@implementation LGetCodeView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.width * 0.65, self.height)];
        self.codeTextField.textColor = [UIColor textBlackColor];
        self.codeTextField.font = [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale]];
        self.codeTextField.keyboardType = UIKeyboardTypePhonePad;
        [self.codeTextField setTextFieldLeftforWidth:15 * [UIScreen zj_screenWScale]];
        self.codeTextField.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        self.codeTextField.layer.borderWidth = 1;
        self.codeTextField.layer.cornerRadius = 4;
        self.codeTextField.layer.masksToBounds = YES;
        self.codeTextField.returnKeyType = UIKeyboardTypeTwitter;
        self.codeTextField.delegate = self;
        [self addSubview:self.codeTextField];
        self.codeButton = [[UIButton alloc] initWithFrame:CGRectMake(self.width * 0.7, 0, self.width * 0.3, self.height)];
        [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.codeButton.backgroundColor = [UIColor themeColor];
        self.codeButton.layer.cornerRadius = 4;
        self.codeButton.layer.masksToBounds = YES;
        [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.codeButton.titleLabel.font = [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale]];
        [self.codeButton addTarget:self action:@selector(code) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.codeButton];
        
    }
    return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (self.maxNumber != 0 && toBeString.length >= self.maxNumber) {
        textField.text = [toBeString substringToIndex:self.maxNumber];
        return NO;
    }
    return YES;
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}
- (void)code{
    
    if (self.delegate ) {
        [self.delegate LGetCodeWith:self click:YES];
    }
}
@end
